<template>
  <n-drawer v-model:show="isDrawer" :width="width" :placement="placement">
    <n-drawer-content :title="title" closable>
      <n-form
        :model="formParams"
        :rules="rules"
        ref="formRef"
        label-placement="left"
        :label-width="100"
      >
        <n-form-item label="上级菜单ID" path="parentId">
          <span>{{ parentId }}</span>
        </n-form-item>
        <n-form-item label="名称" path="label">
          <n-input placeholder="请输入名称" v-model:value="formParams.label" />
        </n-form-item>
        <n-form-item label="图标" path="icon">
          <n-input placeholder="请输入图标" v-model:value="formParams.icon" />
        </n-form-item>
        <n-form-item label="类型" path="type">
          <n-radio-group v-model:value="formParams.type" name="type">
            <n-space>
              <n-radio :value="1">菜单</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item>
        <n-form-item label="路径" path="path">
          <n-input placeholder="请输入路径" v-model:value="formParams.path" />
        </n-form-item>
        <n-form-item label="组件" path="component">
          <n-input placeholder="请输入组件" v-model:value="formParams.component" />
        </n-form-item>
        <n-form-item label="状态" path="status">
          <n-radio-group v-model:value="formParams.status" name="status">
            <n-space>
              <n-radio :value="1">启用</n-radio>
              <n-radio :value="0">禁用</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item>
        <n-form-item label="全局" path="global">
          <n-radio-group v-model:value="formParams.global" name="global">
            <n-space>
              <n-radio :value="1">是</n-radio>
              <n-radio :value="0">否</n-radio>
            </n-space>
          </n-radio-group>
        </n-form-item>
        <n-form-item label="排序" path="sequence">
          <n-input-number
            :show-button="false"
            placeholder="请输入排序"
            v-model:value="formParams.sequence"
          />
        </n-form-item>
        <n-form-item label="描述" path="description">
          <n-input
            type="textarea"
            placeholder="请输入描述"
            v-model:value="formParams.description"
          />
        </n-form-item>
      </n-form>

      <template #footer>
        <n-space>
          <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
          <n-button @click="handleReset">重置</n-button>
        </n-space>
      </template>
    </n-drawer-content>
  </n-drawer>
</template>

<script lang="ts">
  import { defineComponent, reactive, ref, toRefs } from 'vue';
  import { useMessage } from 'naive-ui';
  import { SaveOrUpdate } from './api';

  const rules = {
    parentId: {
      type: 'number',
      required: true,
      message: '请输入上级菜单ID',
      trigger: ['change', 'blur'],
    },
    label: {
      required: true,
      message: '请输入名称',
      trigger: 'blur',
    },
    type: {
      type: 'number',
      required: true,
      message: '请选择类型',
      trigger: ['change', 'blur'],
    },
    path: {
      required: true,
      message: '请输入路径',
      trigger: 'blur',
    },
  };
  export default defineComponent({
    name: 'CreateDrawer',
    components: {},
    props: {
      title: {
        type: String,
        default: '父级地区信息',
      },
      parentId: {
        type: Number,
        default: 0,
      },
      width: {
        type: Number,
        default: 450,
      },
    },
    emits: ['loadMenuTree'],
    setup(props, context) {
      const message = useMessage();
      const formRef: any = ref(null);
      const defaultValueRef = () => ({
        id: null,
        parentId: props.parentId,
        label: null,
        icon: null,
        type: 1,
        path: null,
        component: null,
        status: 1,
        global: 0,
        sequence: 0,
        description: null,
      });

      const state = reactive({
        isDrawer: false,
        subLoading: false,
        formParams: defaultValueRef(),
        placement: 'right',
      });

      function openDrawer() {
        state.isDrawer = true;
      }

      function closeDrawer() {
        state.isDrawer = false;
      }

      function formSubmit() {
        formRef.value.validate(async (errors) => {
          if (!errors) {
            state.formParams.parentId = props.parentId;
            await SaveOrUpdate(state.formParams).then(() => {
              message.success('操作成功', { duration: 3 });
            });
            handleReset();
            closeDrawer();
            context.emit('loadMenuTree');
          } else {
            message.error('请填写完整信息');
          }
        });
      }

      function handleReset() {
        formRef.value.restoreValidation();
        state.formParams = Object.assign(state.formParams, defaultValueRef());
      }

      return {
        ...toRefs(state),
        formRef,
        rules,
        formSubmit,
        handleReset,
        openDrawer,
        closeDrawer,
      };
    },
  });
</script>
